package dr.common.i18n;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

/**
 * 国际化工具类
 * @author 450216797@qq.com
 */
@Component
public class ResKit {

    private static List<Locale> localeList = Arrays.asList(
            new Locale("en_US"),
            new Locale("zh_CN"));
    @Autowired
    private MessageSource messageSource;
    private static MessageSource staticMessageSource;

    @PostConstruct
    public void init() {
        staticMessageSource = messageSource;
    }
    /**
     * 获取单个国际化翻译值
     */
    public static String getMsg(String msgKey) {
        try {
            return staticMessageSource.getMessage(msgKey, null, LocaleContextHolder.getLocale());
        } catch (Exception e) {
            return msgKey;
        }
    }
}